/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entity.Contact;
import entity.RegisteredUser;
import java.io.Serializable;
import java.util.LinkedList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import manager.ContactManager;

/**
 *
 * @author Stefan
 */
@ManagedBean
@RequestScoped
public class ContactsBean implements Serializable {

   
    protected Contact selectedContact;
   
    protected Contact newContact;

    public ContactsBean() {
    }

   
    public Contact getSelectedContact() {
        return selectedContact;
    }

   
    public void setSelectedContact(Contact selectedContact) {
        this.selectedContact = selectedContact;
    }

    public Contact getNewContact() {
        return newContact;
    }

    public void setNewContact(Contact newContact) {
        this.newContact = newContact;
    }

    public void setEditContact(Contact selectedContact) {
        System.out.println("Selected for editing: " + selectedContact.getEmail());
        this.selectedContact = selectedContact;
    }

    /*
     * Save contact action to persist contact entity
     */
    public void saveContact() {
        ContactManager cm = new ContactManager();
        try {
            System.out.println("Saving contact " + selectedContact.getEmail() + " " + selectedContact.getFirstName() + selectedContact.getLastName());
            cm.edit(selectedContact);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   /*
    * Action witch is called when user rpesses on the add button , witch prepares and instanciates a new contact
    */
    public void setPrepareNewContact(RegisteredUser user) {
        newContact = new Contact();
        newContact.setRegisteredUser(user);
    }
    /*
     * Save action for managed beans
     */
    public void saveNewContact() {
        ContactManager cm = new ContactManager();
        try {
            RegisteredUser reguser = newContact.getRegisteredUser();

            System.out.println("REgistered user: " + reguser.getId());
            System.out.println("new contact info" + newContact.getEmail() + newContact.getLastName() + newContact.getFirstName());
            if (newContact.getEmail().matches("[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}") && cm.contactExists(reguser, newContact.getEmail()) == false) {
                cm.create(newContact);
                reguser.getContacts().add(newContact);
                //rum.edit(newContact.getRegisteredUser());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * Save and add another action for managed beans
     */
    public void saveNewContactAddAnother() {
        ContactManager cm = new ContactManager();
        try {
            RegisteredUser reguser = newContact.getRegisteredUser();
            if (reguser.getContacts() == null) {
                reguser.setContacts(new LinkedList<Contact>());
            }

            newContact.getRegisteredUser().getContacts().add(newContact);
            if (newContact.getEmail().matches("[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}") && cm.contactExists(reguser, newContact.getEmail()) == false) {
                cm.create(newContact);
                reguser.getContacts().add(newContact);
                //rum.edit(newContact.getRegisteredUser());
            }
            newContact = new Contact();
            newContact.setRegisteredUser(reguser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * Method for bean to set witch contact is selected for deleting
     * @param contact
     */
    public void setDeleteContact(Contact contact) {
        ContactManager cm = new ContactManager();
        try {
            contact.setDeleted(true);
            contact.getRegisteredUser().getContacts().remove(contact);
            cm.edit(contact);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
